home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-02-13 | 3.3 KB | 107 lines | [TEXT/CWIE] |
- // ZString.h
- // 11/12/96
-
- #ifndef __ZSTRING_H
- #define __ZSTRING_H
-
- #include <Types.h> // for Str255 type (used for Toolbox calls)
-
- class ZString;
-
- // a comparison function analogous to the ANSI strcmp:
- short JStrcmp( const ZString&, const ZString& );
-
- class ZString
- {
- public:
- Str255 buf; // here's the actual data member
-
- //------ constructors & destructor ------
-
- ZString( void ) { buf[0]=0; }
- ZString( const ZString& pStr ) { buf[0]=0; *this = pStr; }
- ZString( const char* pChars ) { buf[0]=0; *this = pChars; }
-
- //------ assignment operators ------
-
- ZString& operator= ( const ZString& pStr );
- ZString& operator= ( const Str255 pPstr );
- ZString& operator= ( const char* pChars );
-
- //------ conversion operators & functions ------
-
- ZString( const long pNum ); // convert int to string
- ZString( const Str255 pPstr ) { buf[0]=0; *this = pPstr; }
- operator ConstStr255Param(void) const { return buf; }
-
- void StuffChars( char* pChars, const int maxlen=255 );
- void StuffPstr( unsigned char* pPstr, const int maxlen=255 );
-
- // I should probably make these guys into member functions:
-
- virtual long IntValue( const ZString& );
- virtual ZString Plural( const ZString& );
- virtual ZString Uppercase( const ZString& );
-
- //------ comparison operators ------
-
- Boolean operator== ( const ZString& s ) const
- { return JStrcmp(*this, s) == 0; }
-
- Boolean operator!= ( const ZString& s ) const
- { return JStrcmp(*this, s) != 0; }
-
- Boolean operator> ( const ZString& s ) const
- { return JStrcmp(*this, s) > 0; }
-
- Boolean operator< ( const ZString& s ) const
- { return JStrcmp(*this, s) < 0; }
-
- Boolean operator>= ( const ZString& s ) const
- { return JStrcmp(*this, s) >= 0; }
-
- Boolean operator<= ( const ZString& s ) const
- { return JStrcmp(*this, s) <= 0; }
-
- //------ length, array-access, substrings ------
-
- short Length(void) const { return buf[0]; }
-
- char& operator[] ( const int i )
- { return (i>=0 && i < buf[0] ? buf[i] : buf[1]); }
-
- // position of a substring (first character is 1)
- int Index( const ZString& pSubstr ) const;
-
- // return a particular element (elements separated by char delimiters)
- ZString Element( const int pElem, const char pDelim=' ' ) const;
-
- // return a substring including 'from' but not 'to' -- again, first char is 1
- ZString Substr( const int from, const int to ) const;
- ZString operator() ( const int from, const int to ) const { return Substr(from,to); }
-
- // shortcuts for specific substrings:
- ZString Left( const int qty=1 ) const { return Substr(1,qty+1); }
- ZString Right( const int qty=1 ) const { return Substr(Length()-qty+1,999); }
-
- //------ get modified versions of the string ------
- ZString Trim() const; // remove leading/trailing whitespace
- ZString Compress() const; // compress multiple spaces/tabs to one space
-
- //------ string concatenation ------
- ZString& operator+= ( const ZString& s );
- ZString operator+ ( const ZString& s2 ) const
- { ZString out(*this); out += s2; return out; }
-
- //------ multiplication, e.g., "\pfoo" * 3 == "\pfoofoofoo" ------
- ZString& operator*= ( const int times )
- { ZString what(*this);
- for (int i=1; i<times; i++) *this += what;
- return *this; }
-
- ZString operator* ( const int times )
- { ZString out(*this); out *= times; return out; }
- };
-
- #endif
-